package com.neusoft.neps.controller;


import com.neusoft.common.domain.Supervisor;
import com.neusoft.neps.service.SupervisorService;
import com.neusoft.util;
import jakarta.annotation.Resource;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Map;
@RestController
public class LoginController {
    @Resource
    SupervisorService service;
    //登录
    @PostMapping("/supervisor_login")
    public Map<String, Object> login(@RequestBody Supervisor supervisor) {
        //登录时，对用户传入的密码进行MD5加密
        String md5Hex = DigestUtils.md5DigestAsHex(supervisor.getPassword().getBytes());
        supervisor.setPassword(md5Hex);
        Map<String, Object> map=service.login(supervisor);
        if ((Boolean) map.get("status")){
            Supervisor allmesg = new Supervisor(service.getById(supervisor.getPhoneNumber()));
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            LocalDate date = LocalDate.parse(allmesg.getBirthday(),formatter);
            LocalDate current = LocalDate.now();
            int age = util.calculateAge(date,current);
            allmesg.setAge(age);
            service.updateById(allmesg);
        }
        return map;
    }

}
